package com.topvision.camerasdk.album.test;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import java.util.List;

/**
 * 頭部控制一些常用方法
 * <p>
 * Title: HeadTrackUtil
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company: topvision
 * </p>
 * 
 * @author jackGu
 * @date 2016-4-29
 */
public class HeadTrackUtil extends Activity {
	// 当前调用者类的绝对位置
	public static final String ABSOLUTE_PACKAGE_NAME = "ABSOLUTE_PACKAGE_NAME";
	static Intent headTrackIntent;
	public static int startId = 0;
	

	/**
	 * 开启头部控制service
	 * <p>
	 * Title: startHeadTrack
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 */
	public static void startHeadTrack(final Context context) {
		if (headTrackIntent == null) {
			headTrackIntent = new Intent(context, TopWindowServiceEpson.class);
			headTrackIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			headTrackIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
		}

		context.startService(headTrackIntent);
	}

	/**
	 * 关闭头部控制service
	 * <p>
	 * Title: stopHeadTrack
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param context
	 */
	public static void stopHeadTrack(Context context) {
		if (headTrackIntent != null) {
			context.stopService(headTrackIntent);
		} else {
			headTrackIntent = new Intent(context, TopWindowServiceEpson.class);
			context.stopService(headTrackIntent);
		}
	}

	/**
	 * 判断当前service是否正在运行
	 * 
	 * @param context
	 * @param serviceName
	 * @return
	 */
	public static boolean isServiceRun(Context context, String serviceName) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningServiceInfo> list = am.getRunningServices(30);
		for (RunningServiceInfo info : list) {
			if (info.service.getClassName().contains(serviceName)) {
				return true;
			}
		}
		return false;
	}

	public static String getActivityByPackage(String absolutePackageName) {
		if (!TextUtils.isEmpty(absolutePackageName)) {
			int index = absolutePackageName.lastIndexOf(".");
			String activityName = absolutePackageName.substring(index + 1,
					absolutePackageName.length());
			return activityName;
		}
		return "";
	}

	/**
	 * 获取最上层Activity的className
	 * 
	 * @param context
	 * @return
	 */
	public static String getCurrentActivityName(Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Activity.ACTIVITY_SERVICE);
		// get the info from the currently running task
		List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
		ComponentName componentInfo = taskInfo.get(0).topActivity;
		return componentInfo.getClassName();
	}

	// public static void showPoint(Context context) {
	// Intent intent = new Intent(context,TopWindowServiceEpson.class);
	// }

}
